<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $settings, $user_info;
global $dbtables, $ksm_allowed;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;	
	
$title = $txt['bnt_main_title'];

//-------------------------------------------------------------------------------------------------

$basefontsize = 0;
$stylefontsize = '8Pt';
$picsperrow = 5;

// Pull player info into $playerinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if (!is_null($playerinfo['cleared_defences']))
{
	echo $txt['bnt_incompletemove'], '<br>';
	echo '<a href="', $scripturl, '?action=blacknova;', $playerinfo['cleared_defences'], '">', $txt['bnt_clicktocontinue'], '</a>';
}
else
{
	// Pull sector info into $sectorinfo.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_universe}
		WHERE sector_id = {int:sector_id}',
		array(
			'db_universe' => $dbtables['universe'],
			'sector_id' => $playerinfo['sector']
		)
	);
	$sectorinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);

	srand((double)microtime() * 1000000);

	// Check for and update planet information.
	if ($playerinfo['on_planet'] == "Y")
	{
		$result = $smcFunc['db_query']('', '
			SELECT planet_id, owner
			FROM {raw:db_planets}
			WHERE planet_id = {int:planet_id}',
			array(
				'db_planets' => $dbtables['planets'],
				'planet_id' => $playerinfo['planet_id']
			)
		);

		if ($smcFunc['db_num_rows']($result) > 0)
		{
			$smcFunc['db_free_result']($result);
			echo '<a href=', $scripturl, '?action=blacknova;sa=planet;planet_id=', $playerinfo['planet_id'], '>', $txt['bnt_clickme'], '</a> ', $txt['bnt_toplanetmenu'], '    <br>';
			redirectexit('action=blacknova;sa=planet;planet_id=' . $playerinfo['planet_id'] . ';id=' . $playerinfo['ship_id']);
			return;
		}
		else
		{
			$smcFunc['db_free_result']($result);
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_ships}
				SET on_planet = "N"
				WHERE ship_id = {int:ship_id}',
				array(
					'db_ships' => $dbtables['ships'],
					'ship_id' => $playerinfo['ship_id']
				)
			);
			echo '<br>', $txt['bnt_nonexistant_pl'], '<br><br>';
		}
	}

	// Are there any planets in the current sector?
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_planets}
		WHERE sector_id = {int:sector_id}',
		array(
			'db_planets' => $dbtables['planets'],
			'sector_id' => $playerinfo['sector']
		)
	);

	$i = 0;
	while ($planets[$i] = $smcFunc['db_fetch_assoc']($result))
	{
		$i++;
	}
	$num_planets = $i;
	$smcFunc['db_free_result']($result);

	// Are there any defences in the current sector?
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_sector_defence}, {raw:db_ships}
		WHERE {raw:db_sector_defence}.sector_id = {int:sector_id}
		AND {raw:db_ships}.ship_id = {raw:db_sector_defence}.ship_id',
		array(
			'db_sector_defence' => $dbtables['sector_defence'],
			'db_ships' => $dbtables['ships'],
			'sector_id' => $playerinfo['sector']
		)
	);

	$i = 0;
	while ($defences[$i] = $smcFunc['db_fetch_assoc']($result))
	{
		$i++;
	}
	$num_defences = $i;
	$smcFunc['db_free_result']($result);

	// Get zone information
	$result = $smcFunc['db_query']('', '
		SELECT zone_id, zone_name
		FROM {raw:db_zones}
		WHERE zone_id = {int:zone_id}',
		array(
			'db_zones' => $dbtables['zones'],
			'zone_id' => $sectorinfo['zone_id']
		)
	);
	$zoneinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);

	/*	
		Get number of links off the current sector and 
		whether the player has visited any of the destination
		sectors
	*/
	$result = $smcFunc['db_query']('', '
		SELECT 	{raw:db_links}.link_id, {raw:db_links}.link_start, 
				{raw:db_links}.link_dest, {raw:db_movement_log}.sector_id
		FROM {raw:db_links}
		LEFT JOIN {raw:db_movement_log}
		ON {raw:db_links}.link_dest = {raw:db_movement_log}.sector_id
		WHERE link_start = {int:sector_id}
		GROUP BY link_dest
		ORDER BY link_dest ASC',
		array(
			'db_links' => $dbtables['links'],
			'db_movement_log' => $dbtables['movement_log'],
			'sector_id' => $playerinfo['sector']
		)
	);

	$i = 0;
	while ($row = $smcFunc['db_fetch_assoc']($result))
	{
		$links[$i] = array(
						'link_dest' => $row['link_dest'],
						'visited' => $row['sector_id']);
		$i++;
	}
	$num_links = $i;
	$smcFunc['db_free_result']($result);


	$shiptypes[0] = "tinyship.gif";
	$shiptypes[1] = "smallship.gif";
	$shiptypes[2] = "mediumship.gif";
	$shiptypes[3] = "largeship.gif";
	$shiptypes[4] = "hugeship.gif";

	$planettypes[0] = "tinyplanet.gif";
	$planettypes[1] = "smallplanet.gif";
	$planettypes[2] = "mediumplanet.gif";
	$planettypes[3] = "largeplanet.gif";
	$planettypes[4] = "hugeplanet.gif";

	// Begin to output the display.
	echo '
							<h2 class="sector_title">', $txt['bnt_sector'], ' ', $playerinfo['sector'], '</h2>
							<input id="current_sector" type="hidden" value="', $playerinfo['sector'], '" />
							<div id="maindisplay_left">';
					
	// Check for special sector names
	if ($zoneinfo['zone_id'] < 5)
		$zoneinfo['zone_name'] = $txt['bnt_zname'][$zoneinfo['zone_id']];	
					
	echo '
								<div id="zone_frame">
									<div class="overlay"></div>
									<div id="zone_content">
										<h4>Zone Type</h4>
										<span id="zone_description">', $zoneinfo['zone_name'], '</span>
										<input type="hidden" id="zone_id" value="', $zoneinfo['zone_id'], '" />
									</div>
								</div>';
								
	// Check for a sector beacon
	if (!empty($sectorinfo['beacon']))
	{
		echo '
								<div id="spacebeacon_frame">
									<div class="overlay"></div>
									<div id="spacebeacon_content">
										<h4>Sector Message</h4>
										<span>', $sectorinfo['beacon'], '</span>
									</div>
								</div>';
	}
	
	echo '
								<div id="warp_links_frame">
									<div class="overlay"></div>
									<div id="warp_links_content">
										<h4>', $txt['bnt_main_warpto'], '</h4>
										<ul>';
										
	if (!$num_links)
		echo '
											<li>', $txt['bnt_no_warplink'], '</li>';
	else
	{
		// Output links to warp to and scan each connecting sector
		for ($i = 0; $i < $num_links; $i++)
		{
			if (is_null($links[$i]['visited']))
			{
				echo '
											<li>
												<a class="warp_new_button ui-corner-all" href="#">', $links[$i]['link_dest'], '</a>
												<a class="scan_button ui-state-default ui-corner-all" href="#"><input type="hidden" value="', $links[$i]['link_dest'], '" />', $txt['bnt_scan'], '</a>
											</li>';
			}
			else
			{
				echo '
											<li>
												<a class="warp_old_button ui-state-default ui-corner-all" href="#">', $links[$i]['link_dest'], '</a>
												<a class="scan_button ui-state-default ui-corner-all" href="#"><input type="hidden" value="', $links[$i]['link_dest'], '" />', $txt['bnt_scan'], '</a>
											</li>';
			}
		}
		
		// Output the full scan button
		echo '
											<li>
												<a class="lrs_button ui-state-default ui-corner-all" href="#"><input type="hidden" value="*" />',$txt['bnt_fullscan'], '</a>
											</li>';
	}

	echo '
										</ul>
									</div>
								</div>
								<div id="rs_move_frame">
									<div class="overlay"></div>
									<div id="rs_move_content">
										<h4>RealSpace Move</h4>
										<input type="text" id="rs_moveto" name="rs_moveto" size="4" maxlength="4" />
										<span id="rs_input_engage" class="bnt_button ui-state-default ui-corner-all">', $txt['bnt_rs_engage_link'], '</span>
									</div>
								</div>
								<div id="move_result">';
	
	// Output last sector button if possible
	if (isset($_GET['last_sector']))
	{
		$energy_scooped = (isset($_GET['energy_scooped'])) ? (float) $_GET['energy_scooped'] : '';
		$last_sector = (isset($_GET['last_sector'])) ? (int) $_GET['last_sector'] : '';
		$triptime = (isset($_GET['triptime'])) ? (int) $_GET['triptime'] : '';
		$move_type = (isset($_GET['move_type'])) ? $_GET['move_type'] : '';
		
		$txt['bnt_rs_turns'] = str_replace("[triptime]", '<strong>' . $triptime . '</strong>', $txt['bnt_rs_turns']);
		
		echo '
									<div id="last_move_frame">
										<div class="overlay"></div>
										<div id="last_move_content">
											<h4>Move Result</h4>
											<ul>
												<li>';
		if ($move_type == 'rs')
			echo '
													<label>Last Sector</label><a class="rs_button ui-state-default ui-corner-all" href="#">', $last_sector, '</a>';
		elseif ($move_type == 'warp')
			echo '
													<label>Last Sector</label><a class="warp_old_button ui-state-default ui-corner-all" href="#">', $last_sector, '</a>';
		else
			echo '
													Error: Unknown move type!';
		
		echo '							
												</li>
												<li>', $txt['bnt_rs_turns'], '</li>';
										
		if (!empty($energy_scooped) && $energy_scooped > 0)
		{		
			$txt['bnt_rs_energy2'] = str_replace("[energy]", '<strong>' . NUMBER($energy_scooped) . '</strong>', $txt['bnt_rs_energy2']);
			echo '
												<li>', $txt['bnt_rs_energy2'], '</li>';
		}
										
		echo '	
											</ul>
									
										</div>
									</div>';
	}
	
	echo '
								</div>';
	
	$i = 0;
	$num_traderoutes = 0;

	// Traderoutes quick link setup query
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_traderoutes}
		WHERE source_type = "P"
		AND source_id = {int:sector_id}
		AND owner = {int:ship_id}
		ORDER BY dest_id ASC',
		array(
			'db_traderoutes' => $dbtables['traderoutes'],
			'sector_id' => $playerinfo['sector'],
			'ship_id' => $playerinfo['ship_id']
		)
	);

	while ($traderoutes[$i] = $smcFunc['db_fetch_assoc']($result))
	{
		$i++;
		$num_traderoutes++;
	}
	$smcFunc['db_free_result']($result);

	// Sector defense trade route query.
	// This is still under developement.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_traderoutes}
		WHERE source_type = "D"
		AND source_id = {int:sector_id}
		AND owner = {int:ship_id}
		ORDER BY dest_id ASC',
		array(
			'db_traderoutes' => $dbtables['traderoutes'],
			'sector_id' => $playerinfo['sector'],
			'ship_id' => $playerinfo['ship_id']
		)
	);

	while ($traderoutes[$i] = $smcFunc['db_fetch_assoc']($result))
	{
		$i++;
		$num_traderoutes++;
	}
	$smcFunc['db_free_result']($result);

	// Personal planet traderoute type query
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_planets}, {raw:db_traderoutes}
		WHERE source_type = "L"
		AND source_id = {raw:db_planets}.planet_id 
		AND {raw:db_planets}.sector_id = {int:sector_id}
		AND {raw:db_traderoutes}.owner = {int:ship_id}
		ORDER BY dest_id ASC',
		array(
			'db_planets' => $dbtables['planets'],
			'db_traderoutes' => $dbtables['traderoutes'],
			'sector_id' => $playerinfo['sector'],
			'ship_id' => $playerinfo['ship_id']
		)
	);
	while ($traderoutes[$i] = $smcFunc['db_fetch_assoc']($result))
	{
		$i++;
		$num_traderoutes++;
	}
	$smcFunc['db_free_result']($result);

	// Corperate planet traderoute type query
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_planets}, {raw:db_traderoutes}
		WHERE source_type = "C"
		AND source_id = {raw:db_planets}.planet_id 
		AND {raw:db_planets}.sector_id = {int:sector_id}
		AND {raw:db_traderoutes}.owner = {int:ship_id}
		ORDER BY dest_id ASC',
		array(
			'db_planets' => $dbtables['planets'],
			'db_traderoutes' => $dbtables['traderoutes'],
			'sector_id' => $playerinfo['sector'],
			'ship_id' => $playerinfo['ship_id']
		)
	);

	while ($traderoutes[$i]= $smcFunc['db_fetch_assoc']($result))
	{
		$i++;
		$num_traderoutes++;
	}

	$smcFunc['db_free_result']($result);

	$i = 0;
	if ($num_traderoutes > 0)
	{
		echo '
								<div id="traderoute_frame">
									<div class="overlay"></div>
									<div id="traderoute_content">
										<h4>', $txt['bnt_traderoutes'], '</h4>
										<ul>';
										
		while ($i < $num_traderoutes)
		{
			echo '
											<li><a href="#" onClick="TradeRouteLink(', $traderoutes[$i]['traderoute_id'], ',1);">';
			
			if ($traderoutes[$i]['source_type'] == 'P')
				echo $txt['bnt_port'];
			elseif ($traderoutes[$i]['source_type'] == 'D')
				echo $txt['bnt_defs'];
			else
			{
				$result = $smcFunc['db_query']('', '
					SELECT name
					FROM {raw:db_planets}
					WHERE planet_id = {int:traderoutes_id}',
					array(
						'db_planets' => $dbtables['planets'],
						'traderoutes_id' =>  $traderoutes[$i]['source_id']
					)
				);
				
				if (!$result || $smcFunc['db_num_rows']($result) == 0)
					echo $txt['bnt_unknown'];
				else
				{
					$planet = $smcFunc['db_fetch_assoc']($result);
					if($planet['name'] == "")
						echo $txt['bnt_unnamed'], ' ';
					else
						echo $planet['name'], ' ';
				}
				
				$smcFunc['db_free_result']($result);
			}

			if ($traderoutes[$i]['circuit'] == '1')
				echo '=&gt;&nbsp;';
			else
				echo ' &lt;=&gt;&nbsp;';
			
			if ($traderoutes[$i]['dest_type'] == 'P')
				echo $traderoutes[$i]['dest_id'];
			elseif ($traderoutes[$i]['dest_type'] == 'D')
				echo $txt['bnt_defsin'] .  $traderoutes[$i]['dest_id'] . "";
			else
			{
				$result = $smcFunc['db_query']('', '
					SELECT name
					FROM {raw:db_planets}
					WHERE planet_id = {int:traderoutes_id}',
					array(
						'db_planets' => $dbtables['planets'],
						'traderoutes_id' =>  $traderoutes[$i]['dest_id']
					)
				);
				
				if(!$result || $smcFunc['db_num_rows']($result) == 0)
					echo $txt['bnt_unknown'];
				else
				{
					$planet = $smcFunc['db_fetch_assoc']($result);
					if($planet['name'] == "")
						echo $txt['bnt_unnamed'];
					else
						echo $planet['name'];
				}
				$smcFunc['db_free_result']($result);
			}
			
			echo ' ('; 
			
			if ($traderoutes[$i]['move_type'] == 'W')
				echo 'W';
			else
				echo 'R';
				
			echo ')</a></li>';				
			$i++;
		}
		echo '
										</ul>
									</div>
								</div>';
	}
	// End output of traderoutes.

	// Begin output of Base, Planets, Ships, Defenses.				
	echo '
									</div>		
									<div id="maindisplay_right">
									<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
										<tr>
											<td valign="top">
												<center><font size="', $basefontsize + 2, '" face="arial" color="white"><b>', $txt['bnt_tradingport'], '</b>:&nbsp;';

	if($sectorinfo['port_type'] != "none")
		echo '<a href=', $scripturl, '?action=blacknova;sa=port>', ucfirst(t_port($sectorinfo['port_type'])), '</a>';
	else
		echo '<b><font size=', $basefontsize+2, '>', $txt['bnt_none'], '</font></b>';

	echo '</font></center>
										<br>
										<center><b><font size="', $basefontsize + 2, '" face="arial" color="white">', $txt['bnt_planet_in_sec'] . $sectorinfo['sector_id'], ':</font></b></center>
										<table border="0" width="100%">
											<tr>';

	if ($num_planets > 0)
	{
		$totalcount=0;
		$curcount=0;
		$i=0;
		while ($i < $num_planets)
		{
			if($planets[$i]['owner'] != 0)
			{
				$result = $smcFunc['db_query']('', '
					SELECT *
					FROM {raw:db_ships}
					WHERE ship_id = {int:planet_id}',
					array(
						'db_ships' => $dbtables['ships'],
						'planet_id' =>  $planets[$i]['owner']
					)
				);
				$planet_owner = $smcFunc['db_fetch_assoc']($result);
				$smcFunc['db_free_result']($result);
				
				$planetavg = $planet_owner['hull'] + $planet_owner['engines'] + $planet_owner['computer'] + $planet_owner['beams'] + $planet_owner['torp_launchers'] + $planet_owner['shields'] + $planet_owner['armor'];
				$planetavg /= 7;

				if($planetavg < 8)
					$planetlevel = 0;
				else if ($planetavg < 12)
					$planetlevel = 1;
				else if ($planetavg < 16)
					$planetlevel = 2;
				else if ($planetavg < 20)
					$planetlevel = 3;
				else
					$planetlevel = 4;
			}
			else
				$planetlevel = 0;

			echo '
												<td align="center" valign="top">
													<div class="planet_view">
														<img src="', $settings['images_url'], '/bnt/', $planettypes[$planetlevel], '" border="0" alt="Planet" />
														<input id="planet_id" type="hidden" value="', $planets[$i]['planet_id'], '">';
			
			if (empty($planets[$i]['name']))
				echo $txt['bnt_unnamed'];
			else
				echo $planets[$i]['name'];

			if ($planets[$i]['owner'] == 0)
				echo '<br />(', $txt['bnt_unowned'], ')';
			else
				echo '<br />(', $planet_owner['character_name'], ')';
			echo '
													</div>
												</td>';
			$i++;
		}
	}
	else
	{
		echo '
												<td align="center" valign="top">
													<br><font color="white" size="', $basefontsize +2, '">', $txt['bnt_none'], '</font>
												</td><br><br>';
	}

	echo '
											</tr>
										</table>
										<center><b><font size=', $basefontsize + 2, ' face="arial" color=white>', $txt['bnt_ships_in_sec'] . $sectorinfo['sector_id'], ':</font></b></center><br>
										<table border="0" width="100%">
											<tr>';

	// Display ships in the sector, unless it is sector 0
	if ($playerinfo['sector'] != 0)
	{	
		$result = $smcFunc['db_query']('', '
			SELECT {raw:db_ships}.*, {raw:db_teams}.team_name, {raw:db_teams}.id
			FROM {raw:db_ships}
				LEFT OUTER JOIN {raw:db_teams}
				ON {raw:db_ships}.team = {raw:db_teams}.id
			WHERE {raw:db_ships}.ship_id <> {int:ship_id}
			AND {raw:db_ships}.sector = {int:sector_id}
			AND {raw:db_ships}.on_planet = "N"',
			array(
				'db_ships' => $dbtables['ships'],
				'db_teams' => $dbtables['teams'],
				'ship_id' =>  $playerinfo['ship_id'],
				'sector_id' =>  $playerinfo['sector']
			)
		);
		
		$totalcount = 0;

		if ($smcFunc['db_num_rows']($result) > 0)
		{
			$curcount = 0;
			echo '
												<td align="center" colspan="99" valign="top">
													<table width="100%" border="0">
														<tr>';
					
			$num_rows = $smcFunc['db_num_rows']($result);
			
			while ($row = $smcFunc['db_fetch_assoc']($result))
			{
				$success = SCAN_SUCCESS($playerinfo['sensors'], $row['cloak']);
				
				if ($success < 5)
					$success = 5;

				if ($success > 95)
					$success = 95;

				$roll = rand(1, 100);

				if ($roll < $success)
				{
					$shipavg = $row['hull'] + $row['engines'] + $row['power'] + $row['computer'] + $row['sensors'] + $row['armor'] + $row['shields'] + $row['beams'] + $row['torp_launchers'] + $row['cloak'];
					$shipavg /= 10;

					if ($shipavg < 8)
						$shiplevel = 0;
					else if ($shipavg < 12)
						$shiplevel = 1;
					else if ($shipavg < 16)
						$shiplevel = 2;
					else if ($shipavg < 20)
						$shiplevel = 3;
					else
						$shiplevel = 4;

					echo '
															<td align="center" valign="top">';

					if ($row['team_name'])
					   echo '	
																<a href="', $scripturl, '?action=blacknova;sa=ship;ship_id=', $row['ship_id'], '"><img src="', $settings['images_url'], '/bnt/', $shiptypes[$shiplevel], '" border="0" /></a><br>
																<font size="', $basefontsize + 1, '" color="#ffffff" face="arial">', $row['ship_name'], '<br>(', $row['character_name'], ')&nbsp;(<font color=#33ff00>', $row['team_name'], '</font>)</font>';
					else
					   echo '
																<a href="', $scripturl, '?action=blacknova;sa=ship;ship_id=', $row['ship_id'], '"><img src="', $settings['images_url'], '/bnt/', $shiptypes[$shiplevel], '" border="0"></a><br>
																<font size="', $basefontsize + 1, '" color="#ffffff" face="arial">', $row['ship_name'], '<br>(', $row['character_name'], ')</font>';

					echo '
															</td>';

					$totalcount++;
				}

				if($smcFunc['db_num_rows']($result) == 0 || $totalcount == 0)
				{
					echo '
															<td align="center">
																<br><font size="2" color="white">', $txt['bnt_none'], '</font>
															</td><br><br>';
					$displayed = true;
					break;
				}
			}
			echo '
														</tr>
													</table>
												</td>';
		}

		if ($smcFunc['db_num_rows']($result) == 0 || $totalcount == 0 && $displayed != true)
		{
			echo '
												<td align="center">
													<br><font size="2" color="white">', $txt['bnt_none'], '</font><br><br>
												</td>';
		}
	}
	else
	{
		echo '
												<td align="center" valign="top">
													<br><font size="2" color=white>', $txt['bnt_sector_0'], '</font>
												</td><br><br>';
	}

	echo '
											</tr>
										</table>';

	if ($num_defences > 0)
	{
		echo '
										<b><center><font face="arial" color="white">', $txt['bnt_sector_def'], '</font></center></b><br>
										<table border="0" width="100%">
											<tr>';
		$totalcount = 0;
		$curcount = 0;
		$i = 0;
		while ($i < $num_defences)
		{
			$defence_id = $defences[$i]['defence_id'];
			echo '
												<td align="center" valign="top">';
			if ($defences[$i]['defence_type'] == 'F')
			{
				echo '
													<a href="', $scripturl, '?action=blacknova;sa=modify_defences;defence_id=', $defence_id, '"><img src="', $settings['images_url'], '/bnt/fighters.gif" border="0" /></a><br>
													<font size="', $basefontsize + 1, '" color="#ffffff" face="arial">';
				$def_type = $txt['bnt_fighters'];
				$mode = $defences[$i]['fm_setting'];
				if ($mode == 'attack')
					$mode = $txt['bnt_md_attack'];
				else
					$mode = $txt['bnt_md_toll'];
				$def_type .= $mode;
			}
			elseif ($defences[$i]['defence_type'] == 'M')
			{
			   echo '
													<a href="', $scripturl, '?action=blacknova;sa=modify_defences;defence_id=', $defence_id, '"><img src="', $settings['images_url'], '/bnt/mines.gif" border="0" /></a><br>
													<font size="', $basefontsize + 1, '" color="#ffffff" face="arial">';
			   $def_type = $txt['bnt_mines'];
			}
			$char_name = $defences[$i]['character_name'];
			$qty = $defences[$i]['quantity'];
			echo $char_name, ' ( ', $qty, ' ', $def_type, ' )';
			echo '</font>
												</td>';

			$i++;
		}
		echo '
											</tr>
										</table>';
	}

	echo '

									</td>
								</tr>
							</table>
							</div>
							<div class="spacer"></div>
							';

	//-------------------------------------------------------------------------------------------------
	
	// New message check at the end to let the page fully load, looks better that way.
	// Check for new message and send javascript alert if there are.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_messages}
		WHERE recp_id = {int:ship_id}
		AND notified = "N"',
		array(
			'db_messages' => $dbtables['messages'],
			'ship_id' => $playerinfo['ship_id']
		)
	);

	if ($smcFunc['db_num_rows']($result) > 0)
	{
		echo '
			<script language="javascript" type="text/javascript">
			alert("', $txt['bnt_youhave'] . $smcFunc['db_num_rows']($result) . $txt['bnt_messages_wait'], '");
			</script>';
		
		/* Mark messages as notified */
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_messages}
			SET notified = "Y"
			WHERE recp_id = {int:ship_id}',
			array(
				'db_messages' => $dbtables['messages'],
				'ship_id' => $playerinfo['ship_id']
			)
		);
	}
	$smcFunc['db_free_result']($result);
}
?>